﻿<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/jsp/common/taglibs.jsp" %>
<html>
<body class="no-skin" scroll=no>
<!-- 以下为内容 -->
<div class="main-content1">
    <!-- /section:basics/content.breadcrumbs -->
    <div class="page-content">
        <!-- PAGE CONTENT BEGINS -->
        <div class="widget-box transparent">
            <!--输入区域-->
            <div class="widget-header widget-header-small">
                <h4 class="widget-title blue smaller">
                    <i class="ace-icon fa fa-rss orange"></i>
                    岗位信息
                </h4>
                <div class="widget-toolbar">
                    <a href="#" class="blue" id="editS" onclick="submitPosAddForm();">
                        <i class="ace-icon fa fa-save "></i>
                        保存岗位信息
                    </a>
                </div>
            </div>
            <div class="widget-body">
                <div class="widget-main padding-8">
                    <!-- #section:pages/profile.feed -->
                    <div id="profile-feed-1" class="profile-feed">
                        <form id="posAddForm" class="form-horizontal" role="form">

                            <div class="space-1">
                                <input name="orgCode" type="hidden" id="orgCode" class="col-xs-6"
                                       value="${organ.orgCode}"/>
                                <input name="addType" type="hidden" id="addType" class="col-xs-6" value="0"/>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       for="form-field-1">所属机构</label>
                                <div class="col-sm-9">
                                    <input name="orgName" type="text" disabled="disabled" id="orgName"
                                           class="col-xs-6" value="${organ.orgName}"/>
                                </div>
                            </div>

                            <div class="space-1"></div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       for="form-field-1">上级岗位</label>
                                <div class="col-sm-9">
                                    <input type="text" id="parentCodeStr" name="parentCodeStr" class="col-xs-6"
                                           readonly onclick="showTree(); return false;"/>
                                    <a href="#" class="btn btn-xs btn-info" onclick="clearContent();">
                                        <i class="fa fa-eraser"></i>
                                    </a>
                                    <input type="hidden" id="parentCode" name="parentCode" class="col-xs-6"/>
                                </div>
                            </div>

                            <div class="space-1"></div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       for="form-field-1">岗位代码</label>
                                <div class="col-sm-9">
                                    <input name="positionCode" type="text" id="positionCode" maxlength="50"
                                           class="col-xs-6"/>
                                </div>
                            </div>

                            <div class="space-1"></div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       for="form-field-1">岗位名称</label>
                                <div class="col-sm-9">
                                    <input type="text" id="positionName" name="positionName" maxlength="100"
                                           class="col-xs-6"/>
                                </div>
                            </div>

                            <div class="space-1"></div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       for="form-field-1">描述</label>
                                <div class="col-sm-9">
                                    <textarea name="remark" rows="5" class="col-xs-6" id="remark"></textarea>
                                </div>
                            </div>

                        </form>
                    </div>
                </div>

                <div class="widget-box transparent" style=" margin-bottom:0;">
                    <div class="widget-header widget-header-small">
                        <h4 class="widget-title blue smaller">
                            <i class="ace-icon glyphicon glyphicon-cog orange"></i>
                            角色维护
                        </h4>

                        <div class="widget-toolbar">
                            <a href="#" class="blue" onclick="showRole();" id="showRoleList"
                               data-toggle="modal">
                                <i class="ace-icon glyphicon glyphicon-plus "></i>
                                添加角色
                            </a>
                        </div>
                    </div>
                </div>

                <div style="overflow-x:auto;   height:100%">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover"
                           style="margin:0; ">
                        <thead>
                        <tr>
                            <th align="center" nowrap="nowrap" class="center">序号</th>
                            <th nowrap="nowrap"><strong>角色名称</strong></th>
                            <th nowrap="nowrap">操作</th>
                        </tr>
                        </thead>
                    </table>
                </div>

                <div class="col-xs-12" style="text-align:center">
                    <!-- 按钮行-->
                    <div class="clearfix form-actions" style="">
                        <button class="btn" type="button" onclick="toBack();">
                            <i class="ace-icon fa fa-undo bigger-110"></i>
                            返 回
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
        <ul id="treeDemo" class="ztree" style="margin-top:0;"></ul>
    </div>

    <script src="${resRoot}/plugin/ace-v1.3/assets/js/jquery.validate.min.js"></script>
    <script src="${resRoot}/plugin/jquery-validation/localization/messages_zh.js"></script>
    <script type="text/javascript">

        //返回
        function toBack() {
            var orgCode = $("#orgCode").val();
            window.location.href = "../../auth/position/toPosList.htm?orgCode=" + orgCode;
        }

        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "positionCode",
                    pIdKey: "parentCode",
                    rootPId: null
                },
                key: {
                    name: "positionName"
                }
            },
            view: {
                selectedMulti: false
            },
            callback: {
                onClick: onClick
            }
        };

        //点击
        function onClick(e, treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                    nodes = zTree.getSelectedNodes(),
                    v = "";
            val = "";
            nodes.sort(function compare(a, b) {
                return a.id - b.id;
            });
            for (var i = 0, l = nodes.length; i < l; i++) {
                v = nodes[i].positionName;
                val = nodes[i].positionCode;
            }

            $("#parentCodeStr").val(v);
            $("#parentCode").val(val);

        }


        //显示下拉树
        function showTree() {
            var posObj = $("#parentCodeStr");
            var posOffset = $("#parentCodeStr").offset();
            $("#menuContent").css({
                left: posOffset.left + "px",
                top: posOffset.top + posObj.outerHeight() + "px"
            }).slideDown("fast");

            $("body").bind("mousedown", onBodyDown);
        }

        //隐藏下拉树
        function hideMenu() {
            $("#menuContent").fadeOut("fast");
            $("body").unbind("mousedown", onBodyDown);
        }


        function onBodyDown(event) {
            if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
                hideMenu();
            }
        }


        //得到树
        $(document).ready(function () {
            var orgCode = $("#orgCode").val();
            $.ajax({
                type: "POST",
                contentType: 'application/json',
                dataType: "json",
                url: contextPath + "/auth/position/getPositionByOrg.json?orgCode=" + orgCode,
                success: function (data) {
                    $.fn.zTree.init($("#treeDemo"), setting, data);
                },
                error: function () {
                    $.Alert("系统错误！");
                }
            });
        });

        function showRole(that) {
            $.Alert("请先保存岗位信息！");
        }

        //清除上级岗位
        function clearContent() {
            $("#parentCodeStr").val("");
            $("#parentCode").val("");
        }


        //处理键盘事件 禁止后退键（Backspace）密码或单行、多行文本框除外
        function forbidBackSpace(e) {
            var ev = e || window.event; //获取event对象   
            var obj = ev.target || ev.srcElement; //获取事件源   
            var t = obj.type || obj.getAttribute('type'); //获取事件源类型   
            //获取作为判断条件的事件类型   
            var vReadOnly = obj.readOnly;
            var vDisabled = obj.disabled;
            //处理undefined值情况   
            vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
            vDisabled = (vDisabled == undefined) ? true : vDisabled;
            //当敲Backspace键时，事件源类型为密码或单行、多行文本的，   
            //并且readOnly属性为true或disabled属性为true的，则退格键失效   
            var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
            //当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效   
            var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
            //判断   
            if (flag2 || flag1) return false;
        }
        //禁止后退键 作用于Firefox、Opera  
        document.onkeypress = forbidBackSpace;
        //禁止后退键  作用于IE、Chrome  
        document.onkeydown = forbidBackSpace;

        //提交表单函数
        function submitPosAddForm() {
            if (validateForm()) {
                $.ajax({
                    url: contextPath + "/auth/position/savePosition.json",
                    type: "post",
                    data: $("#posAddForm").serialize(),
                    async: false,
                    success: function (data) {
                        var msg = eval("(" + data + ")");
                        $.Alert(msg.message, '', addToEditPosition);
                    },
                    error: function () {
                        //$.Alert("服务异常，请稍后尝试！");
                    }
                });
            }
        }

        function addToEditPosition() {
            var positionCode = $("#positionCode").val();
            location.href = "../../auth/position/addToEditPosition.htm?positionCode=" + positionCode;

        }

        //客户简称验证
        //jQuery.validator.addMethod("dealerShtNameCheck",function(value,element){
        //  var reg = /^[0-9a-zA-Z]+$/;
        // return this.optional(element) || reg.test(value);
        //},"客户简称只能为字母或数字");

        //表单验证函数
        function validateForm() {
            return $("#posAddForm").validate({
                errorElement: 'span',
                errorClass: 'help-block',
                focusInvalid: false,
                rules: {
                    "positionCode": {
                        required: true,
                        remote: {
                            url: contextPath + '/auth/position/validCode.json',
                            type: "post",
                            async: false,
                            data: {
                                positionCode: function () {
                                    return $("#positionCode").val()
                                }
                            }
                        }
                    },
                    "positionName": {
                        required: true
                    },
                    "remark": {
                        maxlength: 500
                    }
                },
                messages: {
                    "positionCode": {
                        remote: "该岗位代码已存在，请重新添加"
                    }
                },

                highlight: function (e) {
                    $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
                },

                success: function (e) {
                    $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                    $(e).remove();
                },


                errorPlacement: function (error, element) {
                    if (element.is(':checkbox') || element.is(':radio')) {
                        var controls = element.closest('div[class*="col-"]');
                        if (controls.find(':checkbox,:radio').length > 1) controls.append(error);
                        else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                    }
                    else if (element.is('.select2')) {
                        error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                    }
                    else if (element.is('.chosen-select')) {
                        error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                    }
                    else if (element.is('textarea')) {
                        error.appendTo(element.parent());
                    }
                    else if (element.is('input[type=text]')) {
                        error.appendTo(element.parent());
                    }
                    else error.insertAfter(element.parent());
                }
            }).form();
        }

    </script>
</body>
</html>
